eclipse中如何去除警告:Class is a raw type. References to generic type Class should be parameterized...

您所在的位置:网站首页 generic references eclipse中如何去除警告:Class is a raw type. References to generic type Class should be parameterized...

eclipse中如何去除警告:Class is a raw type. References to generic type Class should be parameterized...

#eclipse中如何去除警告:Class is a raw type. References to generic type Class should be parameterized...| 来源: 网络整理| 查看: 265

转自:https://blog.csdn.net/zwr_1022/article/details/78583872

解决前的源代码:

public class test {public static void main(String args[]) {//入口  try {

   //假设在同一个包中建的一个javaBean: person   Class c = Class.forName("person");//警告出现在这里   try {person factory = (person) c.newInstance();    factory.setName("asdf");    System.out.println(factory.getName());   } catch (InstantiationException e) {    // TODO Auto-generated catch block    e.printStackTrace();   } catch (IllegalAccessException e) {    // TODO Auto-generated catch block    e.printStackTrace();   }  } catch (ClassNotFoundException e) {   // TODO Auto-generated catch block   e.printStackTrace();  }}}

------------------------------

解决方法1:增加编译注解@SuppressWarnings("unchecked")

public class test {@SuppressWarnings("unchecked")//增加在这里public static void main(String args[]) {  try {  Class c = Class.forName("person");//警告出现在这里   try {    person factory = (person) c.newInstance();    factory.setName("asdf");...以下省略

------------------------------

解决方法2:使用泛型通配符

public class test {public static void main(String args[]) {//入口  try {   Class c = Class.forName("person");//这里使用泛型通配符   try {    person factory = (person) c.newInstance();    factory.setName("asdf");    System.out.println(factory.getName());...以下省略



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3